package com.yc.supermarket.service.impl;

import com.yc.supermarket.entity.Admin;
import com.yc.supermarket.entity.User;
import com.yc.supermarket.mapper.UserMapper;
import com.yc.supermarket.service.IUserService;
import com.yc.supermarket.util.MybatisUtil;

import org.apache.ibatis.session.SqlSession;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserServiceImpl implements IUserService {
    private UserMapper mapper;




    @Override
    public int login(User user){
        SqlSession session= MybatisUtil.getSession();
        mapper=session.getMapper(UserMapper.class);
        User user1 =mapper.login(user);
        if(null==user1){
            session.close();
            return 0;
        }else {
            session.close();
            return 1;
        }
    }

    @Override
    public int admlogin(Admin admin){
        SqlSession session= MybatisUtil.getSession();
        mapper=session.getMapper(UserMapper.class);
        Admin admin1 =mapper.admlogin(admin);
        if(null==admin1){
            session.close();
            return 0;
        }else {
            session.close();
            if((admin1.getAdminType().equals("普通管理员"))){
                return 1;
            }else{
                return 2;
            }
        }
    }



    @Override
    public int addUser(User user){
        SqlSession session= MybatisUtil.getSession();
        mapper=session.getMapper(UserMapper.class);
        int a=mapper.addUser(user);
        if(a<=0){
            session.close();
            return 0;
        }else {
            session.close();
            return 1;
        }
    }

    @Override
    public int usermodify(User user) {
        SqlSession session= MybatisUtil.getSession();
        mapper=session.getMapper(UserMapper.class);
        int a =mapper.usermodify(user);
        if(a<=0){
            session.close();
            return 0;
        }else {
            session.close();
            return 1;
        }
    }

    @Override
    public Map<String, Object> find(Map<String, Object> map) {
        SqlSession session = MybatisUtil.getSession();
        mapper = session.getMapper(UserMapper.class);
        List<User> list = mapper.find(map);
        Map<String, Object> m = new HashMap<>();
        if (null == list || list.isEmpty()) {
            m.put("code", 600);
            m.put("msg", "暂无数据");
        } else {
            m.put("code", 200);
            m.put("msg", "成功");
            m.put("data", list);
        }
        MybatisUtil.close(session);
        return m;
    }

}
